﻿@using Microsoft.AspNetCore.Html;
@using Microsoft.Extensions.Options;
@using SimpleIdServer.IdServer.Website.Resources;
@using SimpleIdServer.IdServer.Website.Stores.ClientStore;
@using SimpleIdServer.IdServer.Website.Stores.RealmStore;
@inject IOptions<IdServerWebsiteOptions> Options;
@inject IState<RealmsState> realmsState

<p>
    The <RadzenBadge Text="@Client.ClientType" IsPill="true" /> client named <RadzenBadge Text="@Client.ClientName" IsPill="true" /> has been created. Please use the following properties to configure your client.
</p>

<!-- Client Identifier -->
<div>
    <RadzenText TextStyle="TextStyle.Subtitle2" TagName="TagName.H3">@Global.ClientIdentifier</RadzenText>
    <RadzenTextBox @bind-Value="@Client.ClientId" Class="w-100"></RadzenTextBox>
</div>

<!-- Json Web Key -->
@if(!string.IsNullOrWhiteSpace(Client.JsonWebKeyStr))
{
    <div>
        <RadzenText TextStyle="TextStyle.Subtitle2" TagName="TagName.H3">@Global.Jwk</RadzenText>
        <RadzenTextArea Rows=10 Name="Description" @bind-Value="@Client.JsonWebKeyStr" Class="w-100"></RadzenTextArea>
        <p class="text-muted">
            @((MarkupString)(Global.JwkClientUsageDescription))
        </p>
    </div>
}

<!-- Certificate -->
@if(Client.Pem != null)
{
    <!-- Public Key -->
    <div>
        <RadzenText TextStyle="TextStyle.Subtitle2" TagName="TagName.H3">@Global.PublicKey</RadzenText>
        <RadzenTextArea Rows=10 Name="Description" Value="@Client.Pem.PublicKey" Class="w-100"></RadzenTextArea>
    </div>
    <!-- Private Key -->
    <div>
        <RadzenText TextStyle="TextStyle.Subtitle2" TagName="TagName.H3">@Global.PrivateKey</RadzenText>
        <RadzenTextArea Rows=10 Name="Description" Value="@Client.Pem.PrivateKey" Class="w-100"></RadzenTextArea>
    </div>
}

@switch(Client.ClientType)
{
    case SimpleIdServer.IdServer.WsFederation.WsFederationConstants.CLIENT_TYPE:
        <!-- WSFederation -->
        <div>
            <RadzenLabel Text="@Global.WsFederation" Style="padding: 0px;"></RadzenLabel>
            <RadzenTextBox Value=@GetFederationMetadataUrl() Class="w-100"></RadzenTextBox>
        </div>
        break;
    case SimpleIdServer.IdServer.Saml.Idp.Constants.CLIENT_TYPE:
        <!-- SAML -->
        <div>
            <RadzenLabel Text="@Global.Saml" Style="padding: 0px;"></RadzenLabel>
            <RadzenTextBox Value=@GetSamlMetadataUrl() Class="w-100"></RadzenTextBox>
        </div>
        break;
    default:
        <!-- OpenID Connect -->
        <div>
            <RadzenLabel Text="@Global.OpenIdConnect" Style="padding: 0px;" />
            <RadzenTextBox Value=@GetOpenIdConnectUrl() Class="w-100"></RadzenTextBox>
        </div>
        break;
}

@code {
    string GetOpenIdConnectUrl() => $"{GetBaseUrl()}/.well-known/openid-configuration";
    string GetFederationMetadataUrl() => $"{GetBaseUrl()}/FederationMetadata/2007-06/FederationMetadata.xml";
    string GetSamlMetadataUrl() => $"{GetBaseUrl()}/saml/metadata";
    string GetBaseUrl() => Options.Value.IsReamEnabled ? $"{Options.Value.IdServerBaseUrl}/{realmsState.Value.ActiveRealm}" : Options.Value.IdServerBaseUrl;

    [Parameter]
    public AddClientSuccessAction Client { get; set; }
}